package com.zzl.bonds.ticket.service.impl;

import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Query;

import com.zzl.bonds.ticket.dao.CredirecordDao;
import com.zzl.bonds.ticket.service.CredirecordService;
import com.zzl.bonds.ticket.entity.CredirecordEntity;
import com.zzl.bonds.ticket.dto.CredirecordAddDto;
import com.zzl.bonds.ticket.dto.CredirecordUpdateDto;

/**
 * 授信修改记录
 *
 * @author gull
 * @date 2022-09-14 09:31:58
 */
@Service("credirecordService")
public class CredirecordServiceImpl extends ServiceImpl<CredirecordDao, CredirecordEntity> implements CredirecordService {

    /**
    * 分页查询
    *
    * @param params 查询参数
    * @return 分页数据
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CredirecordEntity> page = this.page(
                new Query<CredirecordEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增
     *
     * @param credirecordAddDto 新增实体
     * @param user 当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void saveCredirecord(CredirecordAddDto credirecordAddDto, User user) throws SysException {
        CredirecordEntity credirecordEntity = new CredirecordEntity();
        BeanUtils.copyProperties(credirecordAddDto, credirecordEntity);
        credirecordEntity.setCreatorId(user.getId());
        if(!this.save(credirecordEntity)){
            throw new SysException("A00001", "保存失败！");
        }
    }

    /**
     * 修改
     *
     * @param credirecordUpdateDto 修改实体
     * @param user 当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void updateCredirecordById(CredirecordUpdateDto credirecordUpdateDto, User user) throws SysException {
        CredirecordEntity credirecordEntity = new CredirecordEntity();
        BeanUtils.copyProperties(credirecordUpdateDto, credirecordEntity);
        credirecordEntity.setUpdaterId(user.getId());
        if(!this.updateById(credirecordEntity)){
            throw new SysException("A00002", "修改失败！");
        }
    }

}